﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.OpenGL.Native;
using System.Drawing;
using Silvermoon.Core;
using Silvermoon.Utils;

namespace Silvermoon.Shapes
{
    public enum ColorOrientation
    {
        Vertical,
        Horizontal,
        Diagonal1,
        Diagonal2,
        TopLeft,
        TopRight,
        BottomLeft,
        BottomRight
    }
    public class GradientShape : Shape
    {
        public GradientShape()
            : base()
        {
        }

        public Color StartColor { get; set; }
        public Color EndColor { get; set; }

        public ColorOrientation Orientation { get; set; }

        protected override Color GetShapeColor()
        {
            return Color.White;
        }

        public int[] GetColorArray(int alpha)
        {
            alpha &= 0xff;
            int a = ColorUtil.SwapRB(StartColor.ToArgb());
            int e = ColorUtil.SwapRB(EndColor.ToArgb());
            if (alpha < 254)
            {
                a = ColorUtil.ModulateAlpha(a, alpha);
                e = ColorUtil.ModulateAlpha(e, alpha);
            }
            switch (Orientation)
            {
                case ColorOrientation.Horizontal:
                    return new int[] { e, e, a, a };

                case ColorOrientation.Vertical:
                    return new int[] { e, a, e, a };

                case ColorOrientation.Diagonal1:
                    return new int[] { a, e, e, a };

                case ColorOrientation.Diagonal2:
                    return new int[] { e, a, a, e };

                case ColorOrientation.BottomRight:
                    return new int[] { a, e, e, e };

                case ColorOrientation.TopRight:
                    int m = ColorUtil.SwapRB(ColorUtil.InterpolateColor(StartColor, EndColor, 128).ToArgb());
                    return new int[] { m, a, e, m };
                // return new int[] { e, a, e, e };

                case ColorOrientation.BottomLeft:
                    return new int[] { e, e, a, e };

                case ColorOrientation.TopLeft:
                    m = ColorUtil.SwapRB(ColorUtil.InterpolateColor(StartColor, EndColor, 128).ToArgb());
                    //return new int[] { e, e, e, a };
                    return new int[] { e, m, m, a };

                default:
                    throw new NotSupportedException();
            }
        }

        short[] rectangleVertex;
        int[] colorArray;

        protected override void Layout()
        {
            rectangleVertex = VertexUtil.SquaredArray(Size);
            colorArray = GetColorArray(255);
            base.Layout();
        }

        public override void Render(Renderer renderer)
        {
            colorArray = GetColorArray(renderer.Alpha * Opacity >> 8);
            gl.Disable(Capability.Texture2D);
            gl.DisableClientState(ClientStateArray.TextureCoord);
            gl.EnableClientState(ClientStateArray.Color);
            gl.ColorPointer(4, glValueType.UnsignedByte, 0, colorArray);
            gl.CheckError();
            gl.VertexPointer(2, glValueType.Short, 0, rectangleVertex);
            gl.CheckError();
            gl.DrawArrays(PrimitiveType.TriangleStrip, 0, rectangleVertex.Length / 2);
            gl.CheckError();
            gl.EnableClientState(ClientStateArray.TextureCoord);
            gl.CheckError();
            gl.Enable(Capability.Texture2D);
          //  gl.MatrixMode(MatrixMode.ModelView);

        }

        protected override Shape CreateClone()
        {
            return new GradientShape();
        }

        protected override void Copy(Shape shape)
        {
            base.Copy(shape);
            GradientShape s = shape as GradientShape;
            s.StartColor = StartColor;
            s.EndColor = EndColor;
            s.Orientation = Orientation;

        }

    }
}
